﻿#Region "Header"

'************************************************************************************************************************
'* Name:   SLAReport
'* Description: GUI Class for all SLA related reports
'* Created On:  26-Oct-2010
'* Created By:  Senthilnathan karuppaiah
'* Last Modified On:
'* Last Modified By:
'* Last Modified Reason:
'************************************************************************************************************************

#End Region 'Header

Imports System.IO
Imports System.Reflection
Imports System.Security
Imports System.Security.Permissions
Imports System.Windows.Forms


Imports Microsoft.Reporting.WinForms
Imports System.Text
''' <summary>
''' VS.NET / SSRS Report viewer control which binds the Master and Sub Work order reports
''' For the sub reports, it initializes the supreportprocessing event handler to set the datasource for each sub report
''' </summary>
''' <remarks></remarks>
Public Class SSRSReportViewer
    Private preProcessed As Boolean = False
#Region "Fields"
    ''' <summary>
    ''' Set the reports into the Dictionary with WorkOrderNumber as key
    ''' this datasource will be used to pick the record for each subreport processing 
    ''' to avoid going to DB/Server for each subreports
    ''' </summary>
    ''' <remarks></remarks>
    Dim dicWo As Dictionary(Of Decimal, DtoWorkOrder)
    ''' <summary>
    ''' Variable to hold the workorder DTO for local manipulation
    ''' </summary>
    ''' <remarks></remarks>
    Dim dtoWo As New DtoWorkOrder()
    ''' <summary>
    ''' Holds the collection of WorkOrderNumber whose value is set in the calling code
    ''' </summary>
    ''' <remarks></remarks>
    Private _WorkOrderList As List(Of Decimal)

#End Region 'Fields

#Region "Constructors"

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    End Sub

#End Region 'Constructors

#Region "Properties"
    ''' <summary>
    ''' Property holds the collection of WorkOrderNumber whose value is set in the calling code
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property WorkOrderList() As List(Of Decimal)
        Get
            Return _WorkOrderList
        End Get
        Set(ByVal value As List(Of Decimal))
            _WorkOrderList = value
        End Set
    End Property

#End Region 'Properties

#Region "Methods"
    ''' <summary>
    ''' VS.NET Reportviewer cpontrol's SubReportProcessing Event handler code to set the datasource for each subreports
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Sub WorkOrderSubreportProcessingEventHandler(ByVal sender As Object, ByVal e As SubreportProcessingEventArgs)

        Dim dtoWos As New List(Of DtoWorkOrder)()
        Dim dtoWo As DtoWorkOrder = dicWo.Item(CDec(e.Parameters.Item("Work_Order_Number").Values.Item(0))) 'New WorkOrderFacade().GetWorkOrderByNumber(CDec(e.Parameters.Item("Work_Order_Number").Values.Item(0)))
        dtoWos.Add(dtoWo)
        e.DataSources.Add(New ReportDataSource("dsWorkOrder", dtoWos))
    End Sub

    ''' <summary>
    ''' 1)Attach Param collection, 2)specify report path, 3)set report processing mode and 4)finally bind
    ''' </summary>
    ''' <param name="reportParamCollection">ReportParameter()</param>
    ''' <param name="reportPath">String</param>
    ''' <remarks></remarks>
    Private Sub BindReport(ByVal reportParamCollection As ReportParameter(), ByVal reportPath As String)

        ' VB.NET
        'Setting the report mode as local meaning the processing will be done in the client's machine / web server i.e. not in the SQL server
        Me.ReportViewer1.ProcessingMode = ProcessingMode.Local
        'ReportViewer1.LocalReport.ReportEmbeddedResource = "EPM." & "WorkOrder.rdlc"
        ReportViewer1.LocalReport.ReportEmbeddedResource = "PagingInSubReports." & reportPath
        'Specify the Report definition language (.rdlc) file path (***Note***: Make sure set the file as embedded resource and copy always in the VS.NET properties)
        'Me.ReportViewer1.LocalReport.ReportPath = reportPath
        'Attach the paramater's collection set ealier
        Me.ReportViewer1.LocalReport.SetParameters(reportParamCollection)
        Dim instance As LocalReport = Me.ReportViewer1.LocalReport
        AddHandler ReportViewer1.LocalReport.SubreportProcessing, AddressOf WorkOrderSubreportProcessingEventHandler
        ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout)
        'Refresh the datasource

        Me.WindowState = FormWindowState.Maximized
        Me.ReportViewer1.Visible = preProcessed
        lblWait.Visible = Not preProcessed
        picWait.Visible = Not preProcessed
        'Refresh the datasource
        Me.ReportViewer1.Refresh()
        'Refresh the report so that it binds
        'Reload the report to show the group pagenation
        If preProcessed Then
            Me.ReportViewer1.RefreshReport()
        End If
    End Sub

    ''' <summary>
    ''' Main Method to render the report
    ''' </summary>
    ''' <remarks></remarks>
    Sub ShowWorkOrderReport()
        Dim dtoWos As New List(Of DtoWorkOrder)()
        dicWo = New Dictionary(Of Decimal, DtoWorkOrder)
        Dim woFacade As IWorkOrderFacade = New WorkOrderFacade()
        dtoWos = woFacade.GetWorkOrders()
        'Query (Object Query) and Set the datasource for Master & Sub Report
        For Each wo As DtoWorkOrder In dtoWos
            dicWo.Add(wo.Work_Order_Number, wo)
        Next
        'Validate for records existence

        'Initializing Report's parameter's collection
        Dim rptParamCollection As ReportParameter() = New ReportParameter(1) {}
        'Report Generated date and time
        rptParamCollection(0) = New ReportParameter("DateReportGenerated", DateAndTime.Now.ToString("MM/dd/yyyy"))
        'Report Title
        rptParamCollection(1) = New ReportParameter("Title", "Work Order Report")
        'Pass the actual datasource, in this case it is a object datasource
        Me.ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("dsWorkOrderMaster", dtoWos))
        '1)Attach Param collection, 2)specify report path, 3)set report processing mode and 4)finally bind
        Me.ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("ReportingServiceUtils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cca1b177d76f2036")
        BindReport(rptParamCollection, "WorkOrderMaster.rdlc")
    End Sub

    ''' <summary>
    ''' For Load which kicks in the ShowReport
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub SSRSReportViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ShowWorkOrderReport()
    End Sub

#End Region 'Methods

    Private Sub ReportViewer1_RenderingComplete(ByVal sender As System.Object, ByVal e As Microsoft.Reporting.WinForms.RenderingCompleteEventArgs) Handles ReportViewer1.RenderingComplete
        Dim oSsrshelper As New ReportingServiceUtils.GroupTotalPage
        If e.Exception Is Nothing Then
            If oSsrshelper.GetGroupWithTotalPageNumber.Count > 0 AndAlso preProcessed = False Then
                preProcessed = True
                ShowWorkOrderReport()
            End If
            'System.Diagnostics.Debug.Print(oSsrshelper.GetRawGroupAndLastPageNumber().Count.ToString())
        End If
    End Sub
End Class